// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import ocsf.server.*;

/**
 * This class overrides some of the methods in the abstract 
 * superclass in order to give more functionality to the server.
 *
 * @author Dr Timothy C. Lethbridge
 * @author Dr Robert Lagani&egrave;re
 * @author Fran&ccedil;ois B&eacute;langer
 * @author Paul Holden
 * @version July 2000
 */
public class EchoServer  extends AbstractServer  implements Serializable
{
  //Class variables *************************************************
  
  /**
   * The default port to listen on.
   */
  final public static int DEFAULT_PORT = 5555;
  /**
   * Create DBMethods instance
   */
  public DBMethods DB =new DBMethods();
  
  
  //Constructors ****************************************************
  
  /**
   * Constructs an instance of the echo server.
   *
   * @param port The port number to connect on.
   */
  public EchoServer(int port) 
  {
    super(port);
  }
      
  //Instance methods ************************************************
  
  /**
   * This method handles any messages received from the client.
   *
   * @param msg The message received from the client.
   * @param client The connection from which the message originated.
   */
  public void handleMessageFromClient
    (Object msg, ConnectionToClient client)
  {	 
	  System.out.println("Handle msg this is : " + msg.toString());
	  SQL s = (SQL)msg;
	  try 
	    {
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","Braude");
			switch (s.sqlNum)
			{
			case 1:  // login student
				msg = DB.checkValidation(conn, s.object, "students");
				break;
			case 2:  // login parent
				msg = DB.checkValidation(conn, s.object, "parents");
				break;
			case 3: // login teacher
				msg = DB.checkValidation(conn, s.object, "teachers");
				break;
			case 4: // add student
				msg = DB.addUser(conn, s.object, "students");
				break;
			case 5: // add parent
				msg = DB.addParent(conn, s.object);
				break;
			case 6: // add teacher
				msg = DB.addUser(conn, s.object, "teachers");
				break;
			case 7: // block parent
				msg = DB.BlockUser(conn, s.object);
				break;
			case 8: // unblock parent
				msg = DB.UnBlockUser(conn, s.object);
				break;
			case 9: // add course
				msg = DB.addCourse(conn, s.object);
				break;
			case 10: // delete student
				msg = DB.delUser(conn, s.object, "students");
				break;
			case 11: // delete parent
				msg = DB.delUser(conn, s.object, "parents");
				break;
			case 12: // delete teacher
				msg = DB.delUser(conn, s.object, "teachers");
				break;
			case 13: // add student to list
				msg = DB.addStudentToList(conn, s.object);
				break;
			case 14: // add class
				msg= DB.addClass(conn, s.object);
				break;
			case 15: // open new semester
				msg = DB.openNewSem(conn, s.object);
				break;
			case 16: // receive assignment
			    msg = DB.receiveAssn(conn, s.object);
			    break;
			case 17: // Define assignment - get teacher courses
				msg = DB.getTeacherCourses(conn, s.object);
				break;
			case 18: // Get Course Name
				msg = DB.getCourseName(conn, s.object);
				break;
			case 19: // add new assignment
				msg = DB.addAss(conn, s.object);
				break;
			case 20: // open existing assignment
				msg = DB.openAssn(conn, s.object);
				break;	
			case 21: // logOut
				msg = DB.logOut(conn, s.object);
				break;	
			case 22: // current semester
				msg = DB.currSem(conn);
				break;	
			case 23: // return courses list
				msg = DB.returnCourses(conn);
				break;	
			case 24: // return class list
				msg = DB.returnClass(conn);
				break;	
			case 25: // return teacher of this course
				msg = DB.returnTechers(conn,s.object);
				break;	
			case 26: // Get class Name
				msg = DB.getClassName(conn, s.object);
				break;
			case 27: // Get Exceptions for changing teacher manager
				msg = DB.TeacherExceptions(conn);
				break;
			case 28: // Get Exceptions for adding student
				msg = DB.ADDExceptions(conn);
				break;
			case 29: // Get Exceptions for removing student
				msg = DB.RMVExceptions(conn);
				break;
			case 30: // Get Teacher Name
				msg= DB.getTeacherName(conn, s.object);
				break;
			case 31: // Change Teacher
				msg = DB.ChangeTeacher(conn,s.object);
				break;
			case 32: // Add Special Student
				msg = DB.SpecialAdd(conn,s.object);
				break;
			case 33: // Remove Special Student
				msg = DB.SpecialRMV(conn,s.object);
				break;
			case 34: // get Teacher data
				msg = DB.TeacherData(conn,s.object);
				break;
			case 35: // Show statistical Date
				msg = DB.GetGrades(conn,s.object);
				break;
			case 36: // get ID for choosing in stats
				msg = DB.GetIDs(conn,s.object);
				break;
			case 37: // get the classes for this teacher
				msg = DB.TeacherClasses(conn,s.object);
				break;
			case 38: // add class to course
				msg = DB.addClassToCourse(conn,s.object);
				break;
			case 39: // check class pre-course
				msg = DB.classPreCourse(conn,s.object);
				break;
			case 40: // get the student name
				msg = DB.getStudentName(conn, s.object);
				break;
			case 41: // get student grades
				msg = DB.StudentsGrades(conn,s.object);
				break;
			case 42: // get where student is enrolled
				msg = DB.StudentEnrolled(conn,s.object);
				break;
			case 43: // get number of assignment
				msg = DB.GetNumberAss(conn,s.object);
				break;
			case 44: // check assignment
				msg = DB.getSubList(conn,s.object);
				break;
			case 45: // submit ass
				msg = DB.SubmitAss(conn,s.object);
				break;
			case 46: // get unit for teacher
				msg = DB.UnitForTeacher(conn,s.object);
				break;
			case 47: // get the entire class in course table
				msg = DB.ClassInCourse(conn);
				break;
			case 48: // get the entire class in course table
				msg = DB.returnDate(conn,s.object);
				break;
			case 49: // return check assignment
				msg = DB.ReturnCheckAss(conn,s.object);
				break;
			case 50: // return check assignment
				msg = DB.AddGrade(conn,s.object);
				break;				
			case 51: // return check assignment
				msg = DB.OpenCheckAss(conn,s.object);
				break;
			case 52: // return check assignment
				msg = DB.ReceiveCheckedAssn(conn,s.object);
				break;
			case 53: // return check assignment
				msg = DB.returnTeachersStudents(conn,s.object);
				break;
				
				
				
				
			case 95: //get dynamic grades
				msg = DB.getDynamicGrades(conn,s.object);
				break;
			case 96: //get all precourses for a course	
				msg = DB.getPreCourse(conn,s.object);
				break;
			case 97: //get the course details
				msg = DB.getCourse(conn,s.object);
				break;
			case 98: //get the entire Studentlist table	
				msg = DB.Studentlist(conn);
				break;
			case 99: // get parent name
				msg = DB.ParentName(conn, s.object);
				break;
			case 100: //get Parent childrens
				msg = DB.ParentChilds(conn,s.object);
				break;
			}
		}
	    catch (SQLException ex) 
		 {/* handle any errors*/
	        System.out.println("SQLException: " + ex.getMessage());
	        System.out.println("SQLState: " + ex.getSQLState());
	        System.out.println("VendorError: " + ex.getErrorCode());
	     }   
	  System.out.println("Message received: " + msg + " from " + client);
	    this.sendToAllClients(msg);
	  }   
  /**
   * This method overrides the one in the superclass.  Called
   * when the server starts listening for connections.
   */
  protected void serverStarted()
  {
    System.out.println
      ("Server listening for connections on port " + getPort());
  }
  
  /**
   * This method overrides the one in the superclass.  Called
   * when the server stops listening for connections.
   */
  protected void serverStopped()
  {
    System.out.println
      ("Server has stopped listening for connections.");
  }
  
  //Class methods ***************************************************
  
  /**
   * This method is responsible for the creation of 
   * the server instance (there is no UI in this phase).
   *
   * @param args[0] The port number to listen on.  Defaults to 5555 
   *          if no argument is entered.
   */
  public static void main(String[] args) 
  {
    int port = 0; //Port to listen on
	try 
	{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (Exception ex) {/* handle the error*/}
    try
    {
      port = Integer.parseInt(args[0]); //Get port from command line
    }
    catch(Throwable t)
    {
      port = DEFAULT_PORT; //Set port to 5555
    }
	
    EchoServer sv = new EchoServer(port);
    
    try 
    {
    	sv.listen(); //Start listening for connections
    } 
    catch (Exception ex) 
    {
      System.out.println("ERROR - Could not listen for clients!");
    }
  }
}
//End of EchoServer class
